// ActionScript file
import com.helloniu.model.UserListModel;
import com.helloniu.controller.UserListController;
import com.helloniu.fl.util.Tool;
import com.helloniu.red5.info.UserInfo;

private var oldInfo:Array = new Array();

private function initApp(e:*):void {
	UserListController.getIns().addView(this);
}

public function updateModel():void {
	var model:UserListModel = UserListController.getIns().getModel() as UserListModel;
	var newInfo:Array = model.getUsers();
	
	// users does not chnage
	if(Tool.compareUsersArray(oldInfo, newInfo)) {
		return;
	}

	oldInfo = Tool.arrayCopy(newInfo);
	
	populate();
}

private function populate():void {
	userListDG.dataProvider = createDP();
}

private function createDP():XMLList {
	var rootList:XMLNode = (new XMLDocument("<users/>")).firstChild;
    
	for(var i:int = 0; i < oldInfo.length; i++) {
    	var userObj:UserInfo = oldInfo[i] as UserInfo;
                
    	var userXML:XMLNode = Tool.creatXMLNode2("user");
        userXML.appendChild(Tool.creatXMLNode("name", userObj.userID));    
        userXML.appendChild(Tool.creatXMLNode("video", userObj.binfo.videoUnit.opened + ""));
        userXML.appendChild(Tool.creatXMLNode("email", ""));

    	rootList.appendChild(userXML);
    }
    
	return new XMLList(rootList).user;
} 